package com.spt.security.dao;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spt.fjj.service.models.common.AnnotationCommonService;
@Service
public class ShiroRoleImpl extends AnnotationCommonService implements IShiroRole {
    private static Logger log = LogManager.getLogger("com.spt.ws.man");
    @Override
    public List<Map<String, Object>> getRoles() {
        String sql = "select rolename,roledesc ,id,createdt,creator,lastupdate,"
                + "(select  to_char(wm_concat(permission )) from t_shiro_permission where id in (select permission_id  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissions,"
                + "(select  to_char(wm_concat(id ))  from T_SHIRO_PERMISSION where id in (select PERMISSION_ID  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissionid "
                + "from T_SHIRO_ROLE u";
        try {
            return this._queryMultex(sql);
        }catch (Exception e) {
            log.error("取角色信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("取角色信息出错，错误信息：" + e.getMessage());
        }
    }
    @Transactional
    @Override
    public Map<String, Object> addRole(Map<String, String> param) throws Exception {
        try {
            String roldId = this._querySingle("select SEQ_SECURITY.nextval from dual");
            String insSql = "insert into t_shiro_role (id,rolename,roledesc,createdt,creator,lastupdate) values(?,?,?,sysdate,?,sysdate)";
            this._update(insSql, roldId,param.get("ROLENAME"),param.get("ROLEDESC"),param.get("creator"));
            String permissions = param.get("PERMISSIONID");
            if(permissions != null) {
                String setPermissionSql = "insert into T_SHIRO_ROLE_PERMISSION ( role_id, permission_id, createdt,creator) values(?,?,sysdate,?)";
                String[] permissionArray = permissions.split(",");
                for(String permissionsId : permissionArray) {
                    this._update(setPermissionSql,roldId, permissionsId, param.get("creator"));
                }
            }
            String sql = "select rolename,roledesc ,id,createdt,creator,lastupdate,"
                    + "(select  to_char(wm_concat(permission )) from t_shiro_permission where id in (select permission_id  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissions,"
                    + "(select  to_char(wm_concat(id ))  from T_SHIRO_PERMISSION where id in (select PERMISSION_ID  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissionid "
                    + "from T_SHIRO_ROLE u where u.id = ?";
            return this._queryMultex(sql, roldId).get(0);
        }catch (Exception e) {
            log.error("增加角色信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("增加角色信息出错，错误信息：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public void delRole(String roleId) throws Exception {
        String sql1 = "delete from t_shiro_role where id = ? ";
        String sql2 = "delete from T_SHIRO_ROLE_PERMISSION where role_id = ?";
        try {
            this._update(sql1,roleId);
            this._update(sql2,roleId);
        }catch(Exception e) {
            log.error("删除角色信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("删除角色信息出错，错误信息：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public Map<String, Object> modifyRole(Map<String, String> param) {
        try {
            String del = "delete from T_SHIRO_ROLE_PERMISSION where role_id = ?";
            String update = "update t_shiro_role set rolename = ?, roledesc=?,lastupdate=sysdate where id = ?";
            this._update(update,param.get("ROLENAME"), param.get("ROLEDESC"), param.get("ID"));
            this._update(del,param.get("ID"));
            String permissions = param.get("PERMISSIONID");
            if(permissions != null) {
                String setPermissionSql = "insert into T_SHIRO_ROLE_PERMISSION (role_id, permission_id, createdt,creator) values(?,?,sysdate,?)";
                String[] permissionArray = permissions.split(",");
                for(String permissionsId : permissionArray) {
                    this._update(setPermissionSql,param.get("ID"), permissionsId, param.get("creator"));
                }
            }
            String sql = "select rolename,roledesc ,id,createdt,creator,lastupdate,"
                    + "(select  to_char(wm_concat(permission )) from t_shiro_permission where id in (select permission_id  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissions,"
                    + "(select  to_char(wm_concat(id ))  from T_SHIRO_PERMISSION where id in (select PERMISSION_ID  from T_SHIRO_ROLE_PERMISSION where role_id=u.id)) permissionid "
                    + "from T_SHIRO_ROLE u where u.id = ?";
            return this._queryMultex(sql, param.get("ID")).get(0);
        }catch(Exception e) {
            log.error("修改角色信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("修改角色信息出错，错误信息：" + e.getMessage());
        }
    }
    @Override
    public List<Map<String, Object>> getRolesSimple() {
            String sql = "select rolename,id,createdt from T_SHIRO_ROLE u";
        try {
            return this._queryMultex(sql);
        }catch (Exception e) {
            log.error("取角色信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("取角色信息出错，错误信息：" + e.getMessage());
        }
    }

}
